﻿using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace R.FarmSmartSleep.RBAC.API.Apliction.Command
{
    public class CaptchaListHandler:IRequestHandler<CaptchaListCommon,Result>
    {
        private readonly IBaseRepository<PhoneCaptcha> phoneCaptchaRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="phoneCaptchaRep"></param>
        public CaptchaListHandler(IBaseRepository<PhoneCaptcha> phoneCaptchaRep)
        {
            this.phoneCaptchaRep = phoneCaptchaRep;
        }

        public async Task<Result> Handle(CaptchaListCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.search };
            try
            {
                var phonDatetime = phoneCaptchaRep.GetAll().Where(c =>c.Captcha==request.captcha && c.Phone == request.phone);
                phonDatetime = phonDatetime.OrderByDescending(c => c.PhoneDate).Take(1);

                if (!phonDatetime.Any())
                {
                    result.Messege = "获取失败";
                }
                else
                {
                    result.Code = (int)CodeEnum.success;
                    result.Messege = "获取成功";  
                }
            }
            catch (Exception)
            {
                result.Messege = "发送失败";
                throw;
            }
            return result;


        }
    }
}
